package starter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JFrame;

import com.jme3.math.Vector3f;

import rollmaterial.Zug;
import strecke.Gleis;
import strecke.Netz;

public class NetzVisualisierung extends Frame {
	
	private static long lastUpdate = System.currentTimeMillis();
	
	//private JFrame window;
	private static Netz netz;

	private static Zug zug;
	private static Zug zug2;

	public static void main (String[] args) throws InterruptedException {
		
		netz = new Netz();
		netz.erzeugeDemoNetz();
		
		zug = new Zug(netz.getGleise().firstElement(), "S1 10000");
		zug2 = new Zug(netz.getGleise().get(8), "S1 20000");
		
		//Fenster erzeugen
		MyFrame frame = new MyFrame( "Gleisplan Anzeige", 1500,1000 ) {
			public void paint(Graphics g) {
				
				//Upcast --> mehr Funktionen in Graphics2D
				Graphics2D g2d=(Graphics2D)g;
				
				//System.out.println("Painting!");
				
				Path2D path = new Path2D.Float();

				Vector<Gleis> gleise = netz.getGleise();
				
				//System.out.println("Anzahl Gleise: " + gleise.size());
				
				for(Gleis gl: gleise)
				{
					//System.out.println(gl.toString());
					
					path.moveTo((gl.getPosition(0).x / 10)+500, (gl.getPosition(0).y / 10)+500);
					int x = 10;
					while(x < gl.getLaenge())
					{
						path.lineTo((gl.getPosition(x).x / 10) + 500, (gl.getPosition(x).y / 10)+500);
						x+=10;
					}
					
				}
				g2d.setColor(Color.black);
				g2d.draw(path);
				
				long newtime = System.currentTimeMillis();
				zug.update((float)(newtime - lastUpdate)/100);
				zug2.update((float)(newtime - lastUpdate)/100);
				lastUpdate = newtime;
				
				Vector3f pos = zug.getPosition();
				Point zugp = new Point((int)pos.x / 10 + 500,(int)pos.y / 10 + 500);
				Rectangle zugrect = new Rectangle(zugp,new Dimension(10, 3));
				g2d.setColor(Color.red);
				g2d.draw(zugrect);
				
				Vector3f pos2 = zug2.getPosition();
				Point zugp2 = new Point((int)pos2.x / 10 + 500,(int)pos2.y / 10 + 500);
				Rectangle zugrect2 = new Rectangle(zugp2,new Dimension(10, 3));
				g2d.setColor(Color.green);
				g2d.draw(zugrect2);

			}
		};
		
		frame.createBufferStrategy(4);
		
		while(true)
		{
			frame.repaint();
			Thread.sleep(100);
		}
		
	}
	
}
